﻿package xzl.edu.cn.practice;

import java.util.Arrays;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Practices {
	/**
	 * 
	 * @param r 圆柱体底面半径
	 * @param h 圆柱体的高
	 * @return 体积
	 */
	public static double getCyVolume(int r, int h) {
		return 3.14 * r * r * h;
	}

	/**
	 * 
	 * @param n 整数
	 * @return 各位数字之和
	 */
	public static int sumDate(int n) {
		int sum = 0;
		while (n != 0) {
			sum += n % 10;
			n /= 10;
		}
		return sum;
	}

	/**
	 * 
	 * @param str 大写字母
	 * @return 小写字母
	 */
	public static char transLetter(String str) {
		return (char) (str.charAt(0) + 32);
	}

	/**
	 * 
	 * @param n 整数
	 * @return 是否被5或6整除
	 */
	public static String judgeDivision(int n) {
		if (n % 5 == 0 && n % 6 == 0)
			return n + " is divisible by both 5 and 6";
		else if (n % 5 != 0 && n % 6 != 0)
			return n + " is not divisible by either 5 or 6";
		else
			return n + " is divisible by 5 or 6, but not both";
	}

	/**
	 * 
	 * @param a数组
	 * @return 排序后的
	 */
	public static void sort3Datas(int a[]) {
		Arrays.sort(a);
	}

	/**
	 * 
	 * @param a 能被5和6同时整数
	 */
	public static void getDivision5and6(int a[]) {
		int ans = 0;
		for (int i = 100; i <= 1000; i++) {
			if (i % 5 == 0 && i % 6 == 0) {
				a[ans++] = i;
			}
		}
	}

	/**
	 * 计算1/3+3/5+...97/99的和
	 */
	public static double sumSequence() {
		double sum = 0;
		for (double i = 1; i <= 97; i++) {
			sum += i / (i + 2) * 1.0;
		}
		return sum;
	}

	/**
	 * 
	 * @param foot英尺
	 * @return米
	 */
	public static double footToMeter(double foot) {
		return foot / 0.305;
	}

	/**
	 * 
	 * @param meter米
	 * @return 英尺
	 */
	public static double meterToFoot(double meter) {
		return 0.305 * meter;
	}

	/**
	 * 全1矩阵
	 */
	public static void printMatrix(int matrix[][], int x) {
		for (int i = 0; i < x; i++) {
			for (int j = 0; j < x; j++)
				matrix[i][j] = 1;
		}
	}

	/**
	 * 
	 * @param begich        开始字符
	 * @param edch          结束字符
	 * @param numberPerLine 以每行numberPerLine个字母显示
	 */
	public static void printChars(char begich, char edch, int numberPerLine) {
		Logger logger = LogManager.getLogger(Practices.class);
		String string = "";
		for (char c = begich; c <= edch; c++)
			string += c;
		int i = 0, j = numberPerLine;
		int x = edch - begich;
		while (x >= numberPerLine) {
			String substr = string.substring(i, j);
			logger.debug(substr);
			i += numberPerLine;
			j += numberPerLine;
			x -= numberPerLine;
		}
		logger.debug(string.substring(i, string.length()));
	}

	/**
	 * 提取数组中不同的数字
	 * 
	 * @param a 原始数组
	 * @return a 提取后的数组
	 */
	public static int[] getSequence(int a[]) {
		Set<Integer> set = new TreeSet<>();
		for (int i = 0; i < a.length; i++)
			set.add(a[i]);
		Iterator<Integer> it = set.iterator();
		int ans = 0;
		a = null;
		a = new int[set.size()];
		while (it.hasNext()) {
			int x = it.next();
			a[ans++] = x;
		}
		return a;
	}

	/**
	 * 统计0-9随机数的个数
	 * 
	 * @return 保存在数组中
	 */
	public static int[] random() {
		int[] sum;
		sum = new int[10];
		for (int i = 0; i < 100; i++) {
			int x = (int) (Math.random() * 10);
			sum[x]++;
		}
		return sum;
	}

	/**
	 * 
	 * @param a数组
	 * @return 返回最小数所在的位置
	 */
	public static int minIndex(int[] a) {
		int min = a[0];
		int minlocal = 0;
		for (int i = 0; i < a.length; i++) {
			if (a[i] < min) {
				min = a[i];
				minlocal = i;
			}
		}
		return minlocal + 1;
	}

	/**
	 * 
	 * @param a二维数组
	 * @return 对角线之和
	 */
	public static int sumDiagonal(int diamatrix[][]) {
		int sum = 0;
		for (int i = 0; i < diamatrix.length; i++) {
			for (int j = 0; j < diamatrix[i].length; j++)
				if (i == j)
					sum += diamatrix[i][j];
		}
		return sum;
	}

	/**
	 * 
	 * @param list1 矩阵1
	 * @param list2 矩阵2
	 * @param list3 矩阵3
	 * @return 矩阵1+矩阵2=矩阵3
	 */
	public static int[][] addMatrix(int list1[][], int list2[][], int list3[][]) {
		for (int i = 0; i < list1.length; i++)
			for (int j = 0; j < list1[i].length; j++)
				list3[i][j] = list1[i][j] + list2[i][j];
		return list3;
	}

	/**
	 * 
	 * @param volue 十进制
	 * @return 二进制码
	 */
	public static String decimalToBinary(int volue) {
		String binstr = "";
		while (volue != 0) {
			int r = volue % 2;
			if (r == 1)
				binstr += "1";
			else
				binstr += "0";
			volue /= 2;
		}
		return new StringBuffer(binstr).reverse().toString();
	}

	/**
	 * 
	 * @param s字符串
	 * @return 字符串中字母的个数
	 */
	public static String countLetters(String str) {
		int sum = 0;
		for (int i = 0; i < str.length(); i++)
			if (str.charAt(i) >= 'A' && str.charAt(i) <= 'z')
				sum++;
		return str + "中字母的个数为：" + sum;
	}

	/**
	 * 
	 * @param binaryString二进制码
	 * @return 十进制
	 */
	public static int parseBinary(String binaryString) {
		int sum = 0;
		int ans = 1;
		for (int i = binaryString.length() - 1; i >= 0; i--) {
			sum += (int) (binaryString.charAt(i) - '0') * ans;
			ans *= 2;
		}
		return sum;
	}
}
